package edu.hhxy.utils;

import java.io.IOException;
import java.io.InputStream;

import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;



public class MyBatisUtil {
    private static SqlSessionFactory factory;

    static{//在静态代码块下，factory只会被创建一次

        try {
            InputStream is = Resources.getResourceAsStream("mybatis3.xml");
            factory = new MybatisSqlSessionFactoryBuilder().build(is,"test");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static SqlSession createSqlSession(){
        return factory.openSession(true);//true 为自动提交事务
    }

    public static void closeSqlSession(SqlSession sqlSession){
        if(null != sqlSession)
            sqlSession.close();
    }
}
